package cn.zjj._01_sync_2;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * package cn.zjj._01_sync;
 *
 * import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
 * import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
 * import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
 * import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
 * import org.apache.rocketmq.common.message.MessageExt;
 *
 * import java.nio.charset.StandardCharsets;
 * import java.util.List;
 */

/**
 * 消费者
 */
public class Consumer {
    public static void main(String[] args) throws Exception {
        //1.消费者对象
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("sync2_consumer_group");
        //2.设置nameserver链接mq
        consumer.setNamesrvAddr("localhost:9876");
        //3.订阅主题topic
        consumer.subscribe("user","login");
        //4.注册消息监听器
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt : list) {
                    String string = new String(messageExt.getBody(), StandardCharsets.UTF_8);

                    System.out.println(string);
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //5.启动消费者
        consumer.start();

    }
}
